package org.dbstar.scheduler.beans;

import java.io.Serializable;
import java.util.Calendar;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.dbstar.scheduler.utils.Utils;


/**
 * Job的参数定义
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-12
 */
public class ParameterDefine implements Serializable {
	private static final long serialVersionUID = -8181974512281347616L;

	private String name;
	private ParameterDefineType type;
	private String value;

	public ParameterDefine(String name, ParameterDefineType type, String value) {
		super();
		this.name = name;
		setType(type);
		setValue(this.type == ParameterDefineType.none ? null : value);
	}

	public ParameterDefine() {
		super();
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setType(ParameterDefineType type) {
		this.type = type == null ? ParameterDefineType.none : type;
	}

	public void setValue(String valuePattern) {
		this.value = StringUtils.isBlank(valuePattern) ? null : valuePattern;
		if (type != ParameterDefineType.none) {
			// 校验valuePattern的合法性
			if (type == ParameterDefineType.pre) {
				if (this.value == null) {
					throw new IllegalArgumentException("预设值类型为["
							+ Utils.getParameterDefineTypeName(ParameterDefineType.pre) + "]时，格式不能为空。");
				}
			} else {
				try {
					DateFormatUtils.format(System.currentTimeMillis(), this.value);
				} catch (Throwable e) {
					throw new IllegalArgumentException("时间格式无法识别：" + e.getLocalizedMessage());
				}
			}
		}
	}

	public String getName() {
		return name;
	}

	public ParameterDefineType getType() {
		return type;
	}

	public String getValue() {
		return value;
	}

	public String getDefineValue(long millis) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millis);
		switch (type) {
		case none:
			return null;
		case pre:
			return value;
		case lastMinute:
			calendar.add(Calendar.MINUTE, -1);
			break;
		case lastHour:
			calendar.add(Calendar.HOUR_OF_DAY, -1);
			break;
		case lastDay:
			calendar.add(Calendar.DATE, -1);
			break;
		case lastWeek:
			calendar.add(Calendar.DATE, -7);
			break;
		case lastMonth:
			calendar.add(Calendar.MONTH, -1);
			break;
		case lastYear:
			calendar.add(Calendar.YEAR, -1);
			break;
		case now:
			break;
		}
		return DateFormatUtils.format(calendar, value);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParameterDefine other = (ParameterDefine) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (type != other.type)
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}
}
